package com.tbgov.controller;

import com.github.pagehelper.PageHelper;
import com.tbgov.entity.Family;
import com.tbgov.entity.FamilyMember;
import com.tbgov.entity.FamilyPovertyOption;
import com.tbgov.entity.pojo.ResultPage;
import com.tbgov.entity.vo.FamilyList;
import com.tbgov.service.IFamilyService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 家庭信息 前端控制器
 * </p>
 *
 * @author 1st
 * @since 2024-11-19
 */
@Tag(name = "家庭信息管理", description = "家庭信息相关接口")
@RestController
@RequestMapping("/family/")
@RequiredArgsConstructor
public class FamilyController {

    final IFamilyService familyService;

    @Operation(summary = "新增/编辑家庭信息")
    @PostMapping("addOrEdit")
    public ResponseEntity<Object> addOrEdit(@RequestBody Family family) {
        familyService.addOrEdit(family);
        return ResponseEntity.ok("success");
    }

    @Operation(summary = "删除家庭信息 可单可批")
    @DeleteMapping("del")
    public ResponseEntity<Object> del(@Parameter(name = "ids", description = "家庭主键ID集合") @RequestParam String ids) {
        familyService.del(Arrays.stream(ids.split(",")).map(Long::parseLong).collect(Collectors.toList()));
        return ResponseEntity.ok("success");
    }

    @Operation(summary = "获取家庭信息列表")
    @GetMapping("list")
    public ResponseEntity<ResultPage<FamilyList>> familyList(@Parameter(name = "pageSize", description = "每页显示条数") Integer pageSize,
                                                             @Parameter(name = "pageNum", description = "当前页码") Integer pageNum,
                                                             @Parameter(name = "sortType", description = "排序类型(0:户号)") Integer sortType,
                                                             @Parameter(name = "sort", description = "排序 0:升序(asc) 1:降序(desc)") Integer sort,
                                                             @Parameter(name = "keyword", description = "关键字") String keyword) {
        PageHelper.startPage(pageNum, pageSize);
        return ResponseEntity.ok(familyService.queryFamilyList(keyword, sortType, sort));
    }

    @Operation(summary = "获取家庭详情")
    @GetMapping({"detail", "auth/detail"})
    public ResponseEntity<FamilyList> queryFamilyDetail(@Parameter(name = "id", description = "家庭主键ID") @RequestParam Long id,
                                                        @Parameter(name = "type", description = "0:简单 1:详细") @RequestParam Integer type, HttpServletRequest request) {
        return ResponseEntity.ok(familyService.queryFamilyDetail(id, type, request));
    }

    @Operation(summary = "获取家庭成员的详情")
    @GetMapping({"member/detail", "auth/member/detail"})
    public ResponseEntity<FamilyMember> queryMemberDetail(@Parameter(name = "id", description = "家庭成员主键ID") @RequestParam Long id, HttpServletRequest request) {
        return ResponseEntity.ok(familyService.queryMemberDetail(id, null, request));
    }

    @Operation(summary = "单个家庭成员新增或编辑")
    @PostMapping("member/addOrEdit")
    public ResponseEntity<Object> addOrEdit(@RequestBody FamilyMember member) {
        familyService.addOrEdit(member, 1);
        return ResponseEntity.ok("success");
    }

    @Operation(summary = "删除家庭成员 可单可批")
    @DeleteMapping("member/del")
    public ResponseEntity<Object> delMember(@Parameter(name = "ids", description = "家庭成员ID集合") @RequestParam String ids) {
        familyService.delMember(Arrays.stream(ids.split(",")).map(Long::parseLong).collect(Collectors.toList()));
        return ResponseEntity.ok("success");
    }

    @Operation(summary = "获取家庭 poverty 配置项")
    @GetMapping("queryPovertyOptions")
    public ResponseEntity<List<FamilyPovertyOption>> queryPovertyOptions() {
        return ResponseEntity.ok(familyService.queryPovertyOptions());
    }

}
